package com.example.petstoreservice.service;

import com.example.petstoreservice.entity.Address;
import com.example.petstoreservice.entity.Product;
import com.example.petstoreservice.entity.Supplier;
import com.example.petstoreservice.mapper.AddressMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AddressService {
    @Autowired
    private AddressMapper addressMapper;
    public boolean insert(Address address){
        int affectRow= addressMapper.insert(address);
        return affectRow>0;
    }
    public List<Address> getByUserName(String userName){
        return addressMapper.getByUserName(userName);
    }
    public boolean delete(Long id){
        int affectRow= addressMapper.delete(id);
        return affectRow>0;
    }
    public boolean update(Address address){
        int affectRow= addressMapper.update(address);
        return affectRow>0;
    }
    public Address getById(Long addressId){
        return addressMapper.getById(addressId);
    }
    public void clearDefaultAddressForUser(String username) {
        List<Address> addresses =addressMapper.getByUserName(username);
        for (Address addr : addresses) {
            if (addr.getMoRen() == 1) {
                addr.setMoRen(0);
                addressMapper.update(addr);
            }
        }
    }
}
